<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>01-state</title>
  </head>
  <body>
    <div id="app"></div>

    <!-- 导入 react 主库 -->
    <script src="../js/react.development.js"></script>
    <!-- 导入 reactDom 扩展库 -->
    <script src="../js/react-dom.development.js"></script>
    <!-- 导入 babel 用于将jsx转为js -->
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
      // 定义类组件
      class Weather extends React.Component {
        constructor(props) {
          super(props);
          // this 指向实例对象
          this.state = {
            isHot: false,
          };

          // 更改changeWeather的this指向
          // bind 方法可以改变this指向并且返回一个新的函数  赋值给实例对象
          this.demo = this.changeWeather.bind(this)
        }

        changeWeather() {
            // class 默认开启方法的局部严格模式 this 指向 undefined
            // 方法中的 this 是谁 调用就是谁
            let { isHot } = this.state;
            this.setState({isHot:!isHot})
        }

        render() {
          let { isHot } = this.state;
          return (
            <div>
              <h1>今天天气很{isHot ? "炎热" : "凉爽"}</h1>
              <button onClick={this.demo}>change</button>
            </div>
          );
        }
      }
      // 渲染虚拟DOM
      ReactDOM.render(<Weather />, document.getElementById("app"));
    </script>
  </body>
</html>
